package com.sc.nft.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.MallRankingListPool;
import com.sc.nft.entity.RankingBoardConf;
import com.sc.nft.entity.vo.*;
import com.sc.nft.service.AdminRankingBoardConfService;
import com.sc.nft.service.MallRankingListPoolService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/ranking/board")
@Api(tags = "后台排行榜管理")
public class AdminRankingBoardController {

    private final AdminRankingBoardConfService rankingBoardConfService;
    private final MallRankingListPoolService mallRankingListPoolService;

    @PostMapping(value = "/addRankingBoard", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加排行榜", notes = "添加排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @Transactional(rollbackFor = Exception.class)
    public Result addAdminRankingBoard(@Valid @RequestBody AddRankingBoardRequest request) {
        rankingBoardConfService.addAdminRankingBoard(BeanUtil.copyProperties(request, RankingBoardConf.class));
        return Result.success();
    }

    @PostMapping(value = "/addRankingBoardRewards", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加排行榜奖励配置", notes = "添加排行榜奖励配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addAdminRankingBoardRewards(@Valid @RequestBody AddRankingBoardRewardsRequest request) {
        rankingBoardConfService.addAdminRankingBoardRewards(request.getId(),request.getRewardsSectionList());
        return Result.success();
    }

//    @PostMapping(value = "/getPageRankingBoardListByIncomeType", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(value = "根据类型获取排行版列表", notes = "添加排行榜奖励配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @SysMemberLogin
//    public PageResult<PageRankingBoardListByIncomeTypeVO> getPageRankingBoardListByIncomeType(@Valid @RequestBody PageRankingBoardListByIncomeTypeRequest request) {
//        return PageResult.success(rankingBoardConfService.getPageRankingBoardListByIncomeType(request.getExpenditureRecordTypeEnum(),request.getPageNo(),request.getPageSize()));
//    }


    @PostMapping(value = "/addRankingBoardIncome", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加排行榜收益配置", notes = "添加排行榜奖励配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addRankingBoardIncome(@Valid @RequestBody AddRankingBoardIncomeRequest request) {
        rankingBoardConfService.addRankingBoardIncome(request.getId(),request.getList());
        return Result.success();
    }


    @PostMapping(value = "/getPageRankingBoardListByType", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取排行版列表", notes = "分页获取排行版列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PageRankingBoardListByTypeVO> getPageRankingBoardListByType(@Valid @RequestBody PageRankingBoardListByTypeRequest request) {
        return PageResult.success(rankingBoardConfService.getPageRankingBoardListByType(request.getType(),request.getPageNo(),request.getPageSize(),request.getName(),request.getBeginTime(),request.getEndTime(),request.getLinkColectionName(),request.getIsEnable()));
    }

    @PostMapping(value = "/getRankingBoardDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "排行版详情", notes = "排行版详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<GetRankingBoardDetailsVO>> getRankingBoardDetails(@Valid @RequestBody GetRankingBoardDetailsRequest request) {
        return Result.success(rankingBoardConfService.getRankingBoardDetails(request.getId(),request.getUserTel()));
    }


    @PostMapping(value = "/updateRankingBoardDetailsStatusByUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排行榜中的某个人拦截发放", notes = "修改排行榜中的某个人拦截发放", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateRankingBoardDetailsStatusByUser(@Valid @RequestBody UpdateRankingBoardDetailsStatusByUserRequest request) {
        rankingBoardConfService.updateRankingBoardDetailsStatusByUser(request.getId(),request.getRanking(),request.getStatus());
        return Result.success();
    }


    @PostMapping(value = "/updateRankingBoardIsEnable", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排行榜是否开启", notes = "修改排行榜是否开启", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateRankingBoardIsEnable(@Valid @RequestBody UpdateRankingBoardIsEnableRequest request) {
        rankingBoardConfService.updateRankingBoardIsEnable(request.getId(),request.getIsEnable());
        return Result.success();
    }

    @PostMapping(value = "/updateRankingBoardIsOpenRewards", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排行榜是否开启奖励", notes = "修改排行榜是否开启奖励", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateRankingBoardIsOpenRewards(@Valid @RequestBody UpdateRankingBoardIsOpenRewardsRequest request) {
        rankingBoardConfService.updateRankingBoardIsOpenRewards(request.getId(),request.getIsOpenRewards());
        return Result.success();
    }

    @PostMapping(value = "/updateRankingBoardSort", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排行榜排序", notes = "修改排行榜排序", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateRankingBoardSort(@Valid @RequestBody UpdateRankingBoardSortRequest request) {
        rankingBoardConfService.updateRankingBoardSort(request.getId(),request.getSort());
        return Result.success();
    }

    @PostMapping(value = "/updateRankingBoardMinimumQuantity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排行榜入榜条件", notes = "修改排行榜入榜条件", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateRankingBoardMinimumQuantity(@Valid @RequestBody UpdateRankingBoardMinimumQuantityRequest request) {
        rankingBoardConfService.updateRankingBoardMinimumQuantity(request.getId(),request.getMinimumQuantity(),request.getRuleSpecification());
        return Result.success();
    }

    @PostMapping(value = "/delRankingBoard", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除排行榜", notes = "删除排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result delRankingBoard(@Valid @RequestBody IdRequest request) {
        rankingBoardConfService.delRankingBoard(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/getRankingBoardRewardsDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "排行版奖励详情", notes = "排行版奖励详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<AdminRankingBoardRewardsDetailsVO>> getRankingBoardRewardsDetails(@Valid @RequestBody IdRequest request) {
        return Result.success(rankingBoardConfService.getRankingBoardRewardsDetails(request.getId()));
    }

    @PostMapping(value = "/getRankingBoardIncomeDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "排行版收益详情", notes = "排行版收益详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<AdminRankingBoardIncomeDetailsVO>> getRankingBoardIncomeDetails(@Valid @RequestBody IdRequest request) {
        return Result.success(rankingBoardConfService.getRankingBoardIncomeDetails(request.getId()));
    }


    @PostMapping(value = "/addMarketValueAccount", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "关联虚拟账号", notes = "关联虚拟账号", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addMarketValueAccount(@Valid @RequestBody AddMarketValueAccountRequest request) {
        rankingBoardConfService.addMarketValueAccount(request.getId(),request.getUserTel());
        return Result.success();
    }

//    @PostMapping(value = "/delRankingBoardIncomeDetails", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(value = "删除排行榜配置的收益", notes = "删除排行榜配置的收益", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @SysMemberLogin
//    public Result delRankingBoardIncomeDetails(@Valid @RequestBody DelRankingBoardIncomeRequest request) {
//        rankingBoardConfService.delRankingBoardIncomeDetails(request.getId(),request.getBeginSection(),request.getEndSection());
//        return Result.success();
//    }

    @PostMapping(value = "/residualPrizePool", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "排行榜池", notes = "排行榜池", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<MallRankingListPool> addMarketValueAccount() {
        MallRankingListPool residualPrizePool = mallRankingListPoolService.getResidualPrizePool();
        return Result.success(residualPrizePool);
    }
}
